import pygame
from pygame.sprite import Sprite


class Ship(Sprite):
    def __init__(self, ai_settings, screen):
        super(Ship, self).__init__()
        # init position of the ship
        self.screen = screen
        self.ai_settings = ai_settings

        self.image = pygame.image.load('images/ship2_0.png')
        self.rect = self.image.get_rect()
        self.screen_rect = self.screen.get_rect()

        self.rect.centery = self.screen_rect.centery
        self.rect.right = self.screen_rect.right

        self.moving_up = False
        self.moving_down = False
        self.moving_left = False
        self.moving_right = False

        self.center = float(self.rect.centery)
        self.right = float(self.rect.right)

    def blitme(self):
        self.screen.blit(self.image, self.rect)

    def update(self):
        if self.moving_up and self.rect.top >= self.screen_rect.top:
            self.center -= self.ai_settings.ship_speed_factor
        elif self.moving_down and self.rect.bottom <= self.screen_rect.bottom:
            self.center += self.ai_settings.ship_speed_factor
        elif self.moving_right and self.rect.right <= self.screen_rect.right:
            self.right += self.ai_settings.ship_speed_factor
        elif self.moving_left and self.rect.left >= self.screen_rect.left:
            self.right -= self.ai_settings.ship_speed_factor

        self.rect.centery = self.center
        self.rect.right = self.right

    def center_ship(self):
        self.rect.centery = self.ai_settings.screen_lenth // 2
        self.rect.right = self.ai_settings.screen_width
